package com.ns.dcjh.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import com.ns.dcjh.constant.Constant;

import java.util.Map;

public class SharedPreferencesUtils {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @SuppressLint("CommitPrefEdits")
    private SharedPreferencesUtils(Context context) {
        if (context != null) {
            sharedPreferences = context.getSharedPreferences(Constant.SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
        }
    }

    private static volatile SharedPreferencesUtils instance;

    public static SharedPreferencesUtils getInstance(Context context) {
        if (instance == null) {
            synchronized (SharedPreferencesUtils.class) {
                if (instance == null) {
                    return new SharedPreferencesUtils(context);
                }
            }
        }
        return instance;
    }

    /**
     * Set Object
     * @param key
     * @param object
     */
    public void setObject(String key, Object object) {
        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object.toString());
        }
        editor.commit();
    }

    /**
     * Get Object
     * @param key
     * @param defaultObject
     * @return
     */
    public Object objectForKey(String key, Object defaultObject) {
        if (sharedPreferences == null)  return "";

        if (defaultObject instanceof String) {
            return sharedPreferences.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sharedPreferences.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sharedPreferences.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sharedPreferences.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sharedPreferences.getLong(key, (Long) defaultObject);
        } else {
            return sharedPreferences.getString(key, null);
        }
    }

    /**
     * Remove Object
     * @param key
     */
    public void removeObjectForKey(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     * Clear All
     */
    public void clearAll() {
        editor.clear();
        editor.commit();
    }

    /**
     * Contain
     * @param key
     * @return
     */
    public Boolean contain(String key) {
        return sharedPreferences.contains(key);
    }

    /**
     * Get All
     * @return
     */
    public Map<String, ?> getAll() {
        return sharedPreferences.getAll();
    }
}
